package com.repair.appraise.service.impl;

import com.repair.api.comment.request.OrderCommRequest;
import com.repair.api.comment.request.param.CommentAddParam;
import com.repair.api.comment.response.CommentOrderResponse;
import com.repair.api.order.service.OrderFacadeService;
import com.repair.appraise.pojo.entity.Comment;
import com.repair.appraise.mapper.CommentMapper;
import com.repair.appraise.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.repair.satoken.utils.StpKit;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author 常月乔
 * @since 2024-10-14
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @DubboReference
    private OrderFacadeService orderFacadeService;


//    @Override
//    public void selectComments(Integer userId) {
//        log.debug("获取用户评论 ={} " + userId);
//        List<Comment> comments = commentMapper.selectList(null);
//        System.out.println("comments = " + comments);
//        Collections.reverse(comments);
//        for (Comment comment : comments) {
//            System.out.println("comment = " + comment);
//        }
//        return;
//    }

//    @Override
//    public void deleteComments(Integer deleted) {
//        log.debug("逻辑删除(1为未删除，0为已删除)" + deleted);
//        Comment comment = new Comment();
//        comment.setDeleted(deleted);
//        commentMapper.insert(comment);
//    }


//    @Override
//    public void filterComments(Integer userId) {
//        log.debug("过滤评论:userId={}" + userId);
//        commentMapper.filterComments(userId);
//    }

//    @Override
//    public void ratingComment(Integer commentId) {
//        log.debug("评分 = {} "+ commentId);
//        commentMapper.ratingComment(commentId);
//    }

    @Override
    public void addComments(CommentAddParam param) {
        Long userId = Long.valueOf((String) StpKit.USER.getLoginId());
        // 创建一个新的Comment对象
        Comment comment = new Comment();
        // 将传入的参数复制到Comment对象中，以简化数据处理
        BeanUtils.copyProperties(param, comment);
        comment.setUserId(userId);
        // 将Comment对象插入数据库
        commentMapper.insert(comment);
        // 在订单表加一条id rpc
        // 创建一个OrderCommRequest对象，用于向订单服务发送请求
        OrderCommRequest orderCommRequest = new OrderCommRequest();
        // 设置评论的ID，这是在插入评论后从数据库中获得的
        orderCommRequest.setCommentId(comment.getId());
        // 设置订单编号，这是从传入的参数中获取的
        orderCommRequest.setOrderNum(param.getOrderNum());
        orderCommRequest.setQuestionDescription(param.getComment());
        // 调用订单服务的commentOrder方法，以在订单表中添加与评论相关的记录
        CommentOrderResponse<String> response = orderFacadeService.commentOrder(orderCommRequest);
        // 注意：此处省略了对response的处理，可能需要根据实际返回值进行后续操作或异常处理



    }

//    @Override
//    public void slectComments(CommentAddParam param) {
//        log.debug("查询评论:commentQuery={}" + param);
//        Comment comment = new Comment();
//        BeanUtils.copyProperties(param, comment);
//        commentMapper.insert(comment);
//
//    }

//    @Override
//    public void deleteCommentt(Long id) {
//        Optional<Comment> optionalComment = commentMapper.findById(id);
//        if (optionalComment.isPresent()) {
//            Comment comment = optionalComment.get();
//            comment.setIsDeleted(false);  // 设置为已删除（0）
//            commentMapper.save(comment);
//        } else {
//            throw new RuntimeException("Comment not found with id: " + id);
//        }
//    }
}